package com.cloudsigns.factorymanager2.engineering.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/27.
 */

public class MediaForPatrolAdapter extends BaseAdapter {
    private Context context;
    private List<MediaBean> datas = new ArrayList<MediaBean>();
    private LayoutInflater inflater;
    private String tag = "";
    private ItemClickListener itemClickListener;


    public MediaForPatrolAdapter(Context context, String tag) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.tag = tag;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    public void setData(List<MediaBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }

    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public MediaBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_media_for_patrol, null);
            holder.tv_top = convertView.findViewById(R.id.tv_top);
            holder.tv_status = convertView.findViewById(R.id.tv_status);
            holder.tv_bottom = convertView.findViewById(R.id.tv_bottom);

            holder.img_view = convertView.findViewById(R.id.img_view);
            holder.img_feedback = convertView.findViewById(R.id.img_feedback);
//            holder.rl_item = convertView.findViewById(R.id.rl_item);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        MediaBean mediaBean = datas.get(position);
        holder.img_view.setTag(mediaBean);
        holder.img_feedback.setTag(mediaBean);
//        holder.rl_item.setTag(mediaBean);
        holder.tv_top.setText(mediaBean.getMediaName());
        holder.tv_bottom.setText(mediaBean.getNumber());

        CommonEnum.CompleteStatus assignCompleteStatus = mediaBean.getAssignCompleteStatus();
        holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FFC000));
        if (null != assignCompleteStatus) {
            if (assignCompleteStatus == CommonEnum.CompleteStatus.WORKING) {
                holder.tv_status.setText("");

            } else if (assignCompleteStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                holder.tv_status.setText("[等待确认]");
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
            } else if (assignCompleteStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                holder.tv_status.setText("[等待确认]");
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
            } else if (assignCompleteStatus == CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE) {
                holder.tv_status.setText("[巡查完成]");
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_74BF73));
            }
        }


        if (mediaBean.getAssignStatus() == CommonEnum.AssignStatus.CREATED) {
            holder.img_feedback.setVisibility(View.GONE);
        } else if (mediaBean.getAssignStatus() == CommonEnum.AssignStatus.EDITED) {
            holder.img_feedback.setVisibility(View.GONE);

        } else if (mediaBean.getAssignStatus() == CommonEnum.AssignStatus.SEND) {
            holder.img_feedback.setVisibility(View.GONE);
        } else if (mediaBean.getAssignStatus() == CommonEnum.AssignStatus.RECEIVED) {
            holder.img_feedback.setVisibility(View.VISIBLE);
        } else if (mediaBean.getAssignStatus() == CommonEnum.AssignStatus.RETURNED) {
            holder.img_feedback.setVisibility(View.GONE);
        } else if (mediaBean.getAssignStatus() == CommonEnum.AssignStatus.COMPLETED) {
            holder.img_feedback.setVisibility(View.GONE);
        } else {
            holder.img_feedback.setVisibility(View.GONE);
        }


//        if (mediaBean.getAssignStatus() == CommonEnum.AssignStatus.CREATED) {
//            holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FFC000));
//            holder.tv_status.setText("[创建/未编辑]");
//        } else if (mediaBean.getAssignStatus() == CommonEnum.AssignStatus.EDITED) {
//            holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_00B050));
//            holder.tv_status.setText("[已编辑]");
//
//        } else if (mediaBean.getAssignStatus() == CommonEnum.AssignStatus.SEND) {
//            holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_00B050));
//            holder.tv_status.setText("[派发中]");
//
//        } else if (mediaBean.getAssignStatus() == CommonEnum.AssignStatus.RECEIVED) {
//            holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_2E75B6));
//            holder.tv_status.setText("[已经接收]");
//        } else if (mediaBean.getAssignStatus() == CommonEnum.AssignStatus.RETURNED) {
//            holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FF0000));
//            holder.tv_status.setText("[退回]");
//        } else if (mediaBean.getAssignStatus() == CommonEnum.AssignStatus.COMPLETED) {
//            holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_00B050));
//            holder.tv_status.setText("[竣工]");
//        }


        holder.img_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != itemClickListener)
                    itemClickListener.itemClick(view, position);
            }
        });

        holder.img_feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != itemClickListener)
                    itemClickListener.itemClick(view, position);
            }
        });


//        holder.rl_item.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (null != itemClickListener)
//                    itemClickListener.itemClick(view, position);
//            }
//        });

        return convertView;
    }

    private class ViewHolder {
        private TextView tv_top;
        private TextView tv_status;
        private TextView tv_bottom;

        private ImageView img_view;
        private ImageView img_feedback;

    }
}
